package com.sw.dubbo.web.interceptor;

import com.sw.study.web.util.RequestContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 定义ip 拦截器
 */
public class IpIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //上下文放入ip
        String addr = request.getRemoteAddr()+request.getMethod();
        System.out.println("放入");
        RequestContext.getContext().setObject("ip",addr);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("12");


    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       //上下文销毁
        System.out.println("销毁");

        RequestContext.getContext().destory();
    }
}
